package ro.cuzma.larry.bookscollection.persistance.xml;

import ro.cuzma.larry.bookscollection.md.Author;
import ro.cuzma.larry.bookscollection.md.Book;
import ro.cuzma.larry.bookscollection.md.Library;
import ro.cuzma.larry.persistance.common.Entity;
import ro.cuzma.larry.persistance.common.EntityIdAndValue;
import ro.cuzma.larry.persistance.xml.XMLNode;
import ro.cuzma.larry.persistance.xml.XMLNodeList;
import ro.cuzma.larry.persistance.xml.XMLSimpleIdValue;
import ro.cuzma.larry.persistance.xml.XMLTagValue;
import ro.cuzma.larry.persistance.xml.XMLTagValueList;
import ro.cuzma.larry.persistance.xml.exception.XMLException;

public class LibraryHelper extends XMLNode {

    private XMLTagValue                                             name;
    private XMLTagValue                                             root;
    private XMLNodeList<AuthorHelper, Author>                       authors;
    private XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> formats;
    private XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> types;
    private XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> languages;
    private XMLTagValueList<EntityIdAndValue<String>>               series;
    private XMLNodeList<BookHelper, Book>                           books;

    public LibraryHelper() {
        super(XMLTags.LIBRARY);
    }

    public LibraryHelper(Library library) throws XMLException {
        super(XMLTags.LIBRARY, library);
    }

    public XMLTagValue getName() {
        return name;
    }

    public XMLTagValue getRoot() {
        return root;
    }

    public XMLNodeList<AuthorHelper, Author> getAuthors() {
        return authors;
    }

    public XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> getLanguages() {
        return languages;
    }

    public XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> getFormats() {
        return formats;
    }

    public XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>> getTypes() {
        return types;
    }

    public XMLNodeList<BookHelper, Book> getBooks() {
        return books;
    }

    public XMLTagValueList<EntityIdAndValue<String>> getSeries() {
        return series;
    }

    private void setAuthorList(Library library) throws XMLException {
        AuthorHelper autH;
        for (Author author : library.getAuthors().getEntityList()) {
            autH = new AuthorHelper(author);
            authors.add(autH);
        }
    }

    private void setLanguagesList(Library library) throws XMLException {
        XMLSimpleIdValue xmlObj;
        for (EntityIdAndValue<String> obj : library.getLanguages().getEntityList()) {
            xmlObj = new XMLSimpleIdValue(XMLTags.LANGUAGE, obj);
            languages.add(xmlObj);
        }
    }

    private void setBookList(Library library) throws XMLException {
        BookHelper bookH;
        for (Book book : library.getBooks().getEntityList()) {
            bookH = new BookHelper(book);
            books.add(bookH);
        }
    }

    private void setFormatList(Library library) throws XMLException {
        XMLSimpleIdValue xmlObj;
        for (EntityIdAndValue<String> obj : library.getFormats().getEntityList()) {
            xmlObj = new XMLSimpleIdValue(XMLTags.FORMAT, obj);
            formats.add(xmlObj);
        }
    }

    private void setTypeList(Library library) throws XMLException {
        XMLSimpleIdValue xmlObj;
        for (EntityIdAndValue<String> obj : library.getTypes().getEntityList()) {
            xmlObj = new XMLSimpleIdValue(XMLTags.TYPE, obj);
            types.add(xmlObj);
        }
    }

    private void setSeriesList(Library library) throws XMLException {
        XMLTagValue xmlObj;
        for (Entity<String> obj : library.getSeries().getEntityList()) {
            xmlObj = new XMLTagValue(XMLTags.SERIES, obj.getId());
            series.add(xmlObj);
        }
    }

    protected void initTags() {
        name = new XMLTagValue(XMLTags.LIBRARY_NAME, null);
        root = new XMLTagValue(XMLTags.LIBRARY_ROOT, null);
        authors = new XMLNodeList<AuthorHelper, Author>(XMLTags.LIBRARY_AUTHORS, XMLTags.AUTHOR);
        series = new XMLTagValueList<EntityIdAndValue<String>>(XMLTags.LIBRARY_SERIES,
                XMLTags.SERIES);
        languages = new XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>>(
                XMLTags.LIBRARY_LANGUAGES, XMLTags.LANGUAGE);
        formats = new XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>>(
                XMLTags.LIBRARY_FORMATS, XMLTags.FORMAT);
        types = new XMLNodeList<XMLSimpleIdValue, EntityIdAndValue<String>>(XMLTags.LIBRARY_TYPES,
                XMLTags.TYPE);

        books = new XMLNodeList<BookHelper, Book>(XMLTags.LIBRARY_BOOKS, XMLTags.BOOK);
        xmlObjects.add(root);
        xmlObjects.add(name);
        xmlObjects.add(series);
        xmlObjects.add(formats);
        xmlObjects.add(types);
        xmlObjects.add(authors);
        xmlObjects.add(languages);
        xmlObjects.add(books);
    }

    @Override
    protected void initWithEntity(Entity<?> entity) throws XMLException {
        Library library = (Library) entity;
        if (library != null) {
            name.setValue(library.getName());
            root.setValue(library.getRoot());
            setSeriesList(library);
            setFormatList(library);
            setTypeList(library);
            setAuthorList(library);
            setBookList(library);
            setLanguagesList(library);
        }
    }

}
